/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * @author  Rezan Achmad
 *          13508104
 * SelectPlayer.java
 *
 * Created on May 15, 2010, 8:13:13 PM
 */

package View.PlayMode;

import Model.Game;
import Model.Map.Map;
import Support.FileSupport;
import Support.IOObject;
import View.MainMenu;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;

/**
 *
 * @author user
 */
public class SelectPlayer extends javax.swing.JFrame {

    /** Creates new form SelectPlayer */
    public SelectPlayer(JFrame parent) {
        this.parent = parent;
        initComponents();
        listfile = FileSupport.GetListFile(".", ".map");
        listmodel = new DefaultListModel();
        for (int i=0;i<listfile.length;++i) {
            listmodel.addElement(listfile[i]);
        }
        mapList.setModel(listmodel);

        player1ComboBox.setEnabled(false);
        player2ComboBox.setEnabled(false);
        player3ComboBox.setEnabled(false);
        player4ComboBox.setEnabled(false);
        okButton.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        selectLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mapList = new javax.swing.JList();
        selectButton = new javax.swing.JButton();
        selectRacePanel = new javax.swing.JPanel();
        player1Label = new javax.swing.JLabel();
        player1ComboBox = new javax.swing.JComboBox();
        player2Label = new javax.swing.JLabel();
        player2ComboBox = new javax.swing.JComboBox();
        player3Label = new javax.swing.JLabel();
        player3ComboBox = new javax.swing.JComboBox();
        player4Label = new javax.swing.JLabel();
        player4ComboBox = new javax.swing.JComboBox();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        selectLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        selectLabel.setText("Select Map : ");

        mapList.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        mapList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(mapList);

        selectButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        selectButton.setText("Select");
        selectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(selectLabel)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(selectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(selectLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(selectButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        player1Label.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player1Label.setText("Player 1 :");

        player1ComboBox.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player1ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Human", "Elf", "Fairy" }));

        player2Label.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player2Label.setText("Player 2 :");

        player2ComboBox.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player2ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Human", "Elf", "Fairy" }));

        player3Label.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player3Label.setText("Player 3 :");

        player3ComboBox.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player3ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Human", "Elf", "Fairy" }));

        player4Label.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player4Label.setText("Player 4 :");

        player4ComboBox.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        player4ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Human", "Elf", "Fairy" }));

        javax.swing.GroupLayout selectRacePanelLayout = new javax.swing.GroupLayout(selectRacePanel);
        selectRacePanel.setLayout(selectRacePanelLayout);
        selectRacePanelLayout.setHorizontalGroup(
            selectRacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, selectRacePanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(selectRacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(selectRacePanelLayout.createSequentialGroup()
                        .addComponent(player3Label)
                        .addGap(18, 18, 18)
                        .addComponent(player3ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(selectRacePanelLayout.createSequentialGroup()
                        .addComponent(player2Label)
                        .addGap(18, 18, 18)
                        .addComponent(player2ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(selectRacePanelLayout.createSequentialGroup()
                        .addComponent(player1Label)
                        .addGap(18, 18, 18)
                        .addComponent(player1ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(selectRacePanelLayout.createSequentialGroup()
                        .addComponent(player4Label)
                        .addGap(18, 18, 18)
                        .addComponent(player4ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        selectRacePanelLayout.setVerticalGroup(
            selectRacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(selectRacePanelLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(selectRacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player1Label)
                    .addComponent(player1ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(selectRacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player2Label)
                    .addComponent(player2ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(selectRacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player3Label)
                    .addComponent(player3ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(selectRacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player4Label)
                    .addComponent(player4ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        okButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(selectRacePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addComponent(selectRacePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(27, 27, 27)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
       parent.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
        String mapName = (String)mapList.getSelectedValue();
        if (mapName!=null) {
            map = (Map)IOObject.Load(mapName);
            enablePlayerLabel(map.GetNumPlayer());
        }
    }//GEN-LAST:event_selectButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
       GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
       GraphicsDevice[] devices = env.getScreenDevices();

       int num = map.GetNumPlayer(); /* get num player */
       Vector<Integer> vrace = new Vector<Integer>(); /* race container */
       int n = 1;

       /* add player 1 race to container */
       n = player1ComboBox.getSelectedIndex()+1;
       vrace.add(n);

       /* add player 2's race to container */
       n = player2ComboBox.getSelectedIndex()+1;
       vrace.add(n);

       if (num==3 || num==4) {
           /* add player 3's race to container */
           n = player3ComboBox.getSelectedIndex()+1;
            vrace.add(n);
       }

       if (num==4) {
           /* add player 4's race to container */
           n = player4ComboBox.getSelectedIndex()+1;
            vrace.add(n);
       }
       Game game = new Game(map, vrace);
       game.setPlayerturn(1);
       Play f = new Play((MainMenu)parent, devices[0], map, vrace, game);
       this.dispose();
    }//GEN-LAST:event_okButtonActionPerformed


    private void enablePlayerLabel(int n) {
        player1ComboBox.setEnabled(true);
        player2ComboBox.setEnabled(true);
        player3ComboBox.setEnabled(true);
        player4ComboBox.setEnabled(true);
        okButton.setEnabled(true);
        if (n==2 || n==3) {
            player4ComboBox.setEnabled(false);
        }

        if (n==2) {
            player3ComboBox.setEnabled(false);
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SelectPlayer(null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JList mapList;
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox player1ComboBox;
    private javax.swing.JLabel player1Label;
    private javax.swing.JComboBox player2ComboBox;
    private javax.swing.JLabel player2Label;
    private javax.swing.JComboBox player3ComboBox;
    private javax.swing.JLabel player3Label;
    private javax.swing.JComboBox player4ComboBox;
    private javax.swing.JLabel player4Label;
    private javax.swing.JButton selectButton;
    private javax.swing.JLabel selectLabel;
    private javax.swing.JPanel selectRacePanel;
    // End of variables declaration//GEN-END:variables

    private JFrame parent;
    private String[] listfile;
    private DefaultListModel listmodel;
    private Map map;
}
